// Package promise 监控window桌面
package promise

import (
	"fmt"
	"os/exec"
	"runtime"
	"time"

	"gitee.com/wang_cyi/TryEverything/public"
)

//YouCanDoIt 周期监督任务
func YouCanDoIt() {
	// 1->4->6

	if runtime.GOOS == "windows" {
		count := 3
		for i := 0; i < count; i++ {
			autoCloseEvernote()
			time.Sleep(1 * time.Second)
			autoCloseChrome() //上班时间：浏览器消耗80%经历 太消耗内存
			time.Sleep(1 * time.Second)
			//autoCloseShadowsocks() //回到家就用电脑Shadowsocks不是学习而上看电影电影小说youtue的
			autoCloseTypora() //白天 80%时间用md 不停copy 记录。忘记你解决这个问题，不是记录这个问题.三天后在总结也不迟
			//autoCloseliteide() //不断处理敲击解决bug 但是上面事情不是计划事情。
			time.Sleep(1 * time.Second)
			autoClosefirefox()
			//autoCloseXmind() //太消耗内存
			time.Sleep(10 * time.Second)

		}
		mouse := "如果不高兴，毫无头绪，但是说不出来，感觉眼睛 head 疲劳，请站起来休息10分钟-- what is  the import thing"
		public.SendMailBy163(mouse)

	}

}

func autoClosefirefox() {
	KillProcess("firefox")

}
func autoCloseEvernote() {
	Kill9Process("EvernoteSubprocess")
}
func autoCloseXmind() {
	KillProcess("XMind")
}
func autoCloseliteide() {
	KillProcess("liteide")
}
func autoCloseTypora() {
	Kill9Process("Typora")
}
func autoCloseShadowsocks() {
	KillProcess("Shadowsocks")
}
func autoCloseChrome() {
	KillProcess("chrome")
}

// KeepAliveWindow KeepAliveWindow
func KeepAliveWindow() bool {

	return true
}

//KillProcess kill调进程
func KillProcess(name string) bool {

	if runtime.GOOS == "windows" {
		name = name + ".exe"
		result, _, _ := IsProcessExist(name)
		if result == false {
			fmt.Println(name, " is stoped ")
			return false
		}
		//arg := []string{"/F", "/IM", name}
		arg := []string{"/IM", name}
		cmd := exec.Command("taskkill", arg...)
		//日志输出到屏幕
		//cmd.Stdout = os.Stdout
		//cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			fmt.Println("Error:cmd.Output ", err)
			return false
		}

		fmt.Println(name, " closed successfully ")

	} else {
		fmt.Println(runtime.GOOS)
	}
	return true
}

//Kill9Process kill调进程
func Kill9Process(name string) bool {

	if runtime.GOOS == "windows" {
		name = name + ".exe"
		result, _, _ := IsProcessExist(name)
		if result == false {
			fmt.Println(name, " is stoped ")
			return false
		}
		arg := []string{"/F", "/IM", name}
		cmd := exec.Command("taskkill", arg...)
		//日志输出到屏幕
		//cmd.Stdout = os.Stdout
		//cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			fmt.Println("Error:cmd.Output ", err)
			return false
		}

		fmt.Println(name, " closed successfully ")

	} else {
		fmt.Println(runtime.GOOS)
	}
	return true
}
